package book.controller;

import book.entity.User;
import book.entity.role_privileges;
import book.service.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/login")
public class LoginController {
    /**
     *
     */
    @Autowired
    private UserServices userService;

    @RequestMapping("/main")
    public String main(){
        return "main";
    }

    @RequestMapping("/validate")
    public String login(User user, HttpSession session){
        User loginUser = userService.login(user);
        if(loginUser != null){
            List<role_privileges> rolePrivileges = loginUser.getRole().getRole_privilegesList();
            for(role_privileges rp : rolePrivileges){
                System.out.println(rp.getRole().getName()+" --- "+rp.getPrivilege().getName());
            }

            session.setAttribute("loginUser",loginUser);
            //重定向
            return "redirect:/login/main";
        }else{
            return "redirect:/login.jsp";
//            return "redirect:login.jsp";
        }
    }
}
